const { HttpRequest, globalData, UserAccess, addUserStat, mfutil, CloudFunction } = getApp();
const app = getApp();
Page({
  data: {
    activeWrap: false,
    userInfo: {},
    tilesList: [],
    css: 0,
    CURRENTPAGE: 1,
    tilesListItemWidth: [],
    router: '/pages/integral-page/integral-page',
    pagetitle: 'Tab首页',
    oarr: [],
    jarr: [],
    contentActive: 'list',
    loadingActive: false
  },
  async onLoad(options) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    let params = app.globalData.params;
    app.globalData.params = '';
    let channel = app.globalData.channel;
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
    }
    let track = UserAccess(UserAccessoptions)

    // 自定义埋点
    let customAccessoptionsd = {
      type: true
    }
    console.log('⾸⻚');
    let trackd = addUserStat(customAccessoptionsd)


    // 自定义埋点
    let customAccessoptions = {
      customType: '⾸⻚'
    }
    console.log('用户进入首页即Dashboard');
    let tracks = addUserStat(customAccessoptions)
    console.log("xxxxxxxxxxxxxxxxxxxparamsparamsparamsparamsxxxxxxxxxxxxxxxxxxxx", params)
    if (params) {
      let type = params[0];
      let id = params[1];
      let url = null;
      console.log("xxxxxxxxxxxxxxxtypetypetypetypetypexxxxxxxxxxxxxxxxxxxxxxxx", type)
      if (type == 'survey' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profiling') {
        //赚积分
        url = `/pages/getIntegral-detail/getIntegral-detail?id=${id}&activetype=unfinished`
      } else if (type == 'reward_sweeps' || type == 'digital_reward_redemption') {  //权益中心
        url = `/pages/equity-detail/equity-detail?&id=${id}&activetype=unfinished`
      } else if (type == 'physical_reward_redemption') {
        url = `/pages/conversion-page/conversion-page?id=${id}&activetype=unfinished`
      } else if (type == 'birthday_bonus_points' || type == 'bonus_points' || type == 'post') {
        //会员特权
        url = `/pages/member-privilege-detail/member-privilege-detail?&id=${id}&activetype=unfinished`
      } else if (type == 'exclusive_sweepstakes') {
        url = `/pages/equity-detail/equity-detail?id=${id}&activetype=unfinished`
      } else if (type == 'earn') {
        url = `/pages/getIntegral-page/getIntegral-page`
      } else if (type == 'redeem') {
        url = `/pages/equity-page/equity-page`
      } else if (type == 'activity') {
        url = `/pages/member-privilege-page/member-privilege-page`
      } else if (type == 'lucky_draw') {
        url = `/pages/draw-prize/draw-prize?id=${id}&activetype=unfinished`
      }
      if (type == 'earn' || type == 'redeem' || type == 'activity') {
        my.reLaunch({
          url
        });
      } else {
        my.navigateTo({
          url
        });
      }
    }
  },
  onShow() {
    this.setData({
      oarr: [],
      jarr: []
    })
    this.initPage('options');
  },
  onHide() {
    this.setData({
      oarr: [],
      jarr: []
    })
  },
  onReachBottom() {
    this.queryTile()
  },
  async initPage(option) {
    this.setData({
      loadingActive: true
    })
    await this.userInfo()
    await this.queryTile(option)
    this.setData({
      loadingActive: false
    })

  },
  async userInfo() {
    //用户信息查询
    let bodyObj1 = {
      // "param": "13800138000",
      // "type": 0,
      // "param": globalData.user_nick,
      "param": globalData.ouid,
      "type": 3
    }
    let path1 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/member/api/tmall/query`;
    let res1 = await HttpRequest(path1, 'POST', '', bodyObj1);
    if (res1.data.resultCode == 0) {
      // res1.data.data.fullName = res1.data.data.fullName!=''? res1.data.data.fullName: '会员';
      globalData.userInfo = res1.data.data;   //存储用户信息
      this.setData({
        menuuserInfo: res1.data.data
      })
    } else {
      my.alert({
        content: res1.data.resultDesc
      })
      // mfutil.launchErrPage(53001)

    }
    //积分查询 
    let bodyObj2 = {
      // "param": "13800138000",
      // "type": 0,
      "param": globalData.ouid,
      "type": 3
    }
    let path2 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/account`;
    let res2 = await HttpRequest(path2, 'POST', '', bodyObj2);
    if (res2.data.resultCode == 0) {
      globalData.userInfo.availablePoint = res2.data.data.availablePoint;
      globalData.userInfo.willExpirePoint = res2.data.data.willExpirePoint;
      this.setData({
        menuavailablePoint: res2.data.data.availablePoint,
        menuwillExpirePoint: res2.data.data.willExpirePoint
      })
    } else {
      my.alert({
        content: res2.data.resultDesc
      })
      // mfutil.launchErrPage(53001)

    }
  },
  async queryTile(options) {
    let { CURRENTPAGE, tilesList, oarr, jarr } = this.data
    //tiles列表页查询
    if (options) {
      CURRENTPAGE = 1
    }
    let parmasObjt = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
      page: CURRENTPAGE,
      size: 10
    }
    let patht = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/available_tiles`;
    let rest = await HttpRequest(patht, 'POST', parmasObjt, {})
    let o = [];
    let j = [];
    if (rest.data.code == 0) {
      rest.data.data.tiles.map((value, index) => {
        if (index % 2 == 0) {
          o.push(value)
        }
      })
      rest.data.data.tiles.map((value, index) => {
        if (index % 2 != 0) {
          j.push(value)
        }
      })
      this.setData({
        oarr: oarr,
        jarr: oarr,
      }, () => {
        this.setData({
          oarr: oarr.concat(o),
          jarr: jarr.concat(j),
          tilesList: tilesList.concat(rest.data.data.tiles),
          CURRENTPAGE: rest.data.data.tiles.length > 0 ? CURRENTPAGE + 1 : CURRENTPAGE
        })
      })
    } else {
      my.alert({
        content: rest.data.msg
      })
      // mfutil.launchErrPage(53001)
    }
    this.setData({
      userInfo: globalData.userInfo
    })
  },
  tileDetailTo(event) {
    console.log(event, "9999999999999");


    let { type, id, title } = event.currentTarget.dataset;
    let url = '';
    if (type == 'survey' || type == 'contest' || type == 'exclusive_content' || type == 'progressive_profiling') {
      //赚积分
      url = `/pages/getIntegral-detail/getIntegral-detail?&id=${id}&activetype=unfinished`
    } else if (type == 'reward_sweeps' || type == 'digital_reward_redemption') {  //权益中心    draw-prize  equity-detail
      url = `/pages/equity-detail/equity-detail?&id=${id}&activetype=unfinished`
    } else if (type == 'physical_reward_redemption') { //conversion-page  store_page
      url = `/pages/conversion-page/conversion-page?&id=${id}&activetype=unfinished`
    } else if (type == 'birthday_bonus_points' || type == 'bonus_points' || type == 'post') {
      //会员特权
      url = `/pages/member-privilege-detail/member-privilege-detail?&id=${id}&activetype=unfinished`
    } else if (type == 'exclusive_sweepstakes') {
      url = `/pages/equity-detail/equity-detail?id=${id}&activetype=unfinished`
    } else if (type == 'lucky_draw') {
      url = `/pages/draw-prize/draw-prize?id=${id}&activetype=unfinished`
    }
    my.navigateTo({
      url
    });
    // /**
    //  *  用户行为记录埋点
    //  */
    // let routerArr = getCurrentPages();
    // let UserAccessoptions = {
    //   grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
    //   name: routerArr[routerArr.length - 1].data.pagetitle,
    //   address: routerArr[routerArr.length - 1].data.router,
    //   String4: ''
    // }
    // let track = UserAccess(UserAccessoptions)
    // 自定义埋点

    // 自定义埋点
    let customAccessoptions = {
      customType: 'Tile点击首页',
      // tileId:id
    }
    // let customAccessoptions = {
    //   customType: title
    // }
    // console.log(title, '过期积分');

    let tracks = addUserStat(customAccessoptions)
  },
  navChange() {
    this.setData({
      activeWrap: true
    })
  },
  navExit() {
    this.setData({
      activeWrap: false
    })
  },



});
